package com.bm.beautifulmouse.controller;

import com.bm.beautifulmouse.effect.Effect;
import com.bm.beautifulmouse.effect.ParticleEffect;
import com.bm.beautifulmouse.effect.SnowEffect;
import com.bm.beautifulmouse.effect.StarEffect;
import com.bm.beautifulmouse.effect.HeartEffect;
import com.bm.beautifulmouse.effect.PawEffect;
import com.bm.beautifulmouse.effect.FirecrackerEffect;
import com.bm.beautifulmouse.effect.RainbowTrailEffect;
import com.bm.beautifulmouse.model.EffectConfig;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;

public class EffectController {
    @FXML private Pane effectPane;
    @FXML private ComboBox<String> effectTypeCombo;
    @FXML private ColorPicker colorPicker;
    @FXML private Slider sizeSlider;
    @FXML private Slider speedSlider;
    @FXML private Slider rangeSlider;
    @FXML private Slider particleCountSlider;

    // 标签引用
    @FXML private Label sizeLabel;
    @FXML private Label speedLabel;
    @FXML private Label rangeLabel;
    @FXML private Label particleCountLabel;

    private Effect currentEffect;
    private EffectConfig config;

    @FXML
    public void initialize() {
        // 初始化配置
        config = new EffectConfig();
        currentEffect = new SnowEffect(); // 设置初始特效为雪花特效
        // 设置下拉框样式和内容
        effectTypeCombo.getItems().addAll("雪花特效", "粒子特效", "星星特效", "心形特效", "猫爪特效", "爆竹特效", "彩虹尾巴");
        effectTypeCombo.setValue("雪花特效");
        effectTypeCombo.getStyleClass().add("custom-combo-box");

        // 设置滑块的默认值和样式
        sizeSlider.setValue(1.0);
        speedSlider.setValue(1.0);
        rangeSlider.setValue(1.0);
        particleCountSlider.setValue(5.0);

        // 添加滑块样式类
        sizeSlider.getStyleClass().add("custom-slider");
        speedSlider.getStyleClass().add("custom-slider");
        rangeSlider.getStyleClass().add("custom-slider");
        particleCountSlider.getStyleClass().add("custom-slider");

        // 设置颜色选择器默认颜色和样式
        colorPicker.setValue(Color.WHITE);
        colorPicker.getStyleClass().add("custom-color-picker");

        // 添加事件监听器
        setupEventListeners();

        // 添加鼠标移动事件监听
        effectPane.setOnMouseMoved(event -> {
            if (currentEffect instanceof RainbowTrailEffect ||
                    currentEffect instanceof StarEffect) {  // 添加StarEffect判断
                // 更新配置
                config.setColor(colorPicker.getValue());
                config.setSize(sizeSlider.getValue());
                config.setSpeed(speedSlider.getValue());
                config.setRange(rangeSlider.getValue());
                config.setParticleCount((int) particleCountSlider.getValue());

                // 生成特效
                currentEffect.generate(effectPane, event.getX(), event.getY(), config);
            }
        });
    }

    private void setupEventListeners() {
        // 添加点击事件监听器
        effectPane.setOnMouseClicked(event -> {
            if (currentEffect != null) {
                // 更新配置
                config.setColor(colorPicker.getValue());
                config.setSize(sizeSlider.getValue());
                config.setSpeed(speedSlider.getValue());
                config.setRange(rangeSlider.getValue());
                config.setParticleCount((int) particleCountSlider.getValue());

                // 生成特效
                currentEffect.generate(effectPane, event.getX(), event.getY(), config);
            }
        });

        // 特效类型切换监听
        effectTypeCombo.setOnAction(e -> {
            switch(effectTypeCombo.getValue()) {
                case "雪花特效":
                    currentEffect = new SnowEffect();
                    break;
                case "粒子特效":
                    currentEffect = new ParticleEffect();
                    break;
                case "星星特效":
                    currentEffect = new StarEffect();
                    break;
                case "心形特效":
                    currentEffect = new HeartEffect();
                    break;
                case "猫爪特效":
                    currentEffect = new PawEffect();
                    break;
                case "爆竹特效":
                    currentEffect = new FirecrackerEffect();
                    break;
                case "彩虹尾巴":
                    currentEffect = new RainbowTrailEffect();
                    break;
            }
        });

        // 其他控件值变化监听器
        colorPicker.setOnAction(e -> config.setColor(colorPicker.getValue()));
        sizeSlider.valueProperty().addListener((obs, old, newVal) ->
                config.setSize(newVal.doubleValue()));
        speedSlider.valueProperty().addListener((obs, old, newVal) ->
                config.setSpeed(newVal.doubleValue()));
        rangeSlider.valueProperty().addListener((obs, old, newVal) ->
                config.setRange(newVal.doubleValue()));
        particleCountSlider.valueProperty().addListener((obs, old, newVal) ->
                config.setParticleCount(newVal.intValue()));
    }

    /**
     * 重置为默认设置
     */
    @FXML
    private void resetToDefault() {
        colorPicker.setValue(Color.WHITE);
        sizeSlider.setValue(1.0);
        speedSlider.setValue(1.0);
        rangeSlider.setValue(50);
        particleCountSlider.setValue(5);
    }

    /**
     * 设置炫丽预设
     */
    @FXML
    private void setFancyPreset() {
        colorPicker.setValue(Color.MAGENTA);
        sizeSlider.setValue(1.5);
        speedSlider.setValue(1.8);
        rangeSlider.setValue(75);
        particleCountSlider.setValue(30);
    }

    /**
     * 设置柔和预设
     */
    @FXML
    private void setSoftPreset() {
        colorPicker.setValue(Color.SKYBLUE);
        sizeSlider.setValue(0.8);
        speedSlider.setValue(0.6);
        rangeSlider.setValue(40);
        particleCountSlider.setValue(15);
    }

    /**
     * 设置控件初始值
     */
    private void setupControls() {
        // 设置特效类型选项
        effectTypeCombo.getItems().addAll(
            "星星特效",
            "彩虹尾巴",
            "粒子特效",
            "雪花特效",
            "心形特效"
        );
        effectTypeCombo.setValue("星星特效");

        // 设置默认值
        resetToDefault();
    }

    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 大小滑块监听
        sizeSlider.valueProperty().addListener((obs, oldVal, newVal) -> {
            config.setSize(newVal.doubleValue());
            sizeLabel.setText(String.format("%.1f", newVal.doubleValue()));
        });

        // 速度滑块监听
        speedSlider.valueProperty().addListener((obs, oldVal, newVal) -> {
            config.setSpeed(newVal.doubleValue());
            speedLabel.setText(String.format("%.1f", newVal.doubleValue()));
        });

        // 范围滑块监听
        rangeSlider.valueProperty().addListener((obs, oldVal, newVal) -> {
            config.setRange(newVal.doubleValue());
            rangeLabel.setText(String.format("%.0f", newVal.doubleValue()));
        });

        // 粒子数量滑块监听
        particleCountSlider.valueProperty().addListener((obs, oldVal, newVal) -> {
            config.setParticleCount(newVal.intValue());
            particleCountLabel.setText(String.format("%d", newVal.intValue()));
        });

        // 颜色选择器监听
        colorPicker.valueProperty().addListener((obs, oldVal, newVal) -> {
            config.setColor(newVal);
        });
    }
}